package com.dw.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dw.entity.User;
import com.dw.service.UserService;
import com.google.gson.Gson;

/**
 * 
 * @ClassName: UserController.java
 * @Description: 
 * @author: wangxingchao
 */
@Controller
@RequestMapping(value="/user")
public class UserController {
	@Autowired
	UserService userService;
	
	
	private static Log log = LogFactory.getLog(User.class);
	
	@RequestMapping(value = "/toList")
	public String toList(){
		return "userlist";
	}
	
	/**
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public	String list(HttpServletRequest request, HttpServletResponse response)  throws Exception{
		
		
		
		response.setContentType("text/html;charset=utf-8");
		//ResultString result = new ResultString();
		String dateString = null;
		Gson g = new Gson();
		try {
			List<User> lists = userService.getAllUser();
			dateString = g.toJson(lists);
			
			//result.setCode("true");
			//result.setDate(dateString);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error("获取数据失败", e);
			}
			//result.setCode("false");
		}
		PrintWriter out = response.getWriter();
		out.print(dateString);
		return null;
	}
	
	@RequestMapping(value="/add")
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		User user = new User();
		user.setName("阿亮");
		user.setAddress("前海");
		user.setAge(20);
		user.setBirthday(new Date());
		user.setPassword("123");
		
		userService.addUser(user);
		out.print("添加用户");
		return null;
	}

}
